package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.adapter.AnchorAdapter;
import com.fanhao.dllo.soundfm.entity.AnchorResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.tools.MyOnclickListener;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by dllo on 16/1/11.
 * 主播界面
 */
public class AnchorFragment extends BaseFragment implements MyOnclickListener {
    private RecyclerView anchorRv;
    private AnchorAdapter anchorAdapter;
    private String url = "http://mobile.ximalaya.com/m/explore_user_index?device=android&page=1";

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_anchor;
    }

    @Override
    protected void initView() {
        anchorRv = findView(R.id.rv_anchor);
        anchorRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        anchorAdapter = new AnchorAdapter(getActivity());
        anchorAdapter.setMyOnclickListener(this);
        anchorRv.setAdapter(anchorAdapter);
    }

    @Override
    protected void initData() {
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        AnchorResult result = gson.fromJson(response,
                                AnchorResult.class);
                        if (result != null) {
                            List<AnchorResult.ListEntity> anchorListEntity = result.getList();
                            if (anchorListEntity != null && anchorListEntity.size() > 0) {
                                anchorAdapter.addAnthorData(anchorListEntity);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    public void myListener(long uid, int pos) {
        goToNextPlayList(new AnchorInforFragment(uid));
    }

    @Override
    public void myNoListener(String content,int p) {

    }
}
